// 上传接口
var express = require('express'); //引入express模块
var router = express.Router(); //引入express路由
const path = require('path'); //引入path模块
const fs = require('fs'); //引入fs模块
const { formidable } = require('formidable'); //使用formidable库上传图片
// 引入数据模块
const conn = require('./db/conn')
const {baseURL} =require('../config/config.js')
router.post('/', (req, res) => {
    const form = formidable({
        uploadDir: path.resolve(__dirname, '../public/images'),//设置上传文件保存的路径
        keepExtensions: true,//保持上传文件的扩展名
    });
    form.parse(req, (err, fields, { file }) => {
        if (err) {
            next(err);
            return;
        }
        let sid = fields.id[0];
        let setUrl = `s_url='${baseURL+file[0].newFilename}'`;
        const sql = `UPDATE student SET ${setUrl} WHERE sid='${sid}'`;
        conn.query(sql, (error, results) => {
            if (error) throw error
            console.log(results);
            if (results.affectedRows > 0) {
                res.send({ code: 0, success: true, msg: '上传成功', file })
            }
        })
    })

});

module.exports = router;